package cn.momoky.ccool.runner;

import cn.momoky.ccool.pojo.User;
import cn.momoky.ccool.service.UserService;
import cn.momoky.ccool.utils.UUID;
import lombok.AllArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class ApplicationRunnerImpl implements ApplicationRunner {

    private static final User user = new User()
            .setName("momoky")
            .setPassword("123456")
            .setEmail("778259229@qq.com")
            .setCapacity(2L<< 42)
            .setUsed(0L);

    private final UUID uuid;
    private final UserService userService;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        if (!uuid.isExists()) {
            uuid.set(-1L);
        }

        User u = userService.findByNameOrEmail(user.getName(), user.getEmail());
        if (u != null) return;
        user.setId(uuid.get());
        userService.add(user);
    }
}
